import React, {Component, PropTypes} from 'react';
import assign from 'object-assign';
import {createServerState} from './helpers/states';
import get_photodetail_routes from './containers/photoDetail/routes';
import get_helloWorld_routes from './containers/helloWorld/routes';
import get_userdetail_routes from './containers/user/routes';
import {renderToString} from 'react-dom/server';
import {matchRoutes} from 'react-router-config';

import Root from './Root';

import LayoutApp from './containers/layout/App';
const layoutJson = {
    App : LayoutApp
}

const routes = assign({},get_helloWorld_routes,get_photodetail_routes,get_userdetail_routes);
module.exports = {
    get: (params) => {
        let routeRenders = ()=>{
            let enterWithoutLoginRouteKeys = Object.keys(routes);
            return enterWithoutLoginRouteKeys.map(function (item, index) {
                let obj = routes[item];
                return obj;
            })
        }
        // const routesConfig = [{
        //     path: '/n/m/photoDetail/:id',
        //     exact: true,
        //     component: Photodeteim,
        //     thunk: () => {
        //
        //     },
        // }, {
        //     path: 'n/m/user',
        //     component: Photodeteim,
        //     thunk: () => {
        //     },
        // }];
        return new Promise((resolve, reject) => {
            createServerState().then(state => {
                let branch = matchRoutes(routeRenders(), params.url);
                let promises = branch.map(({route, match}) => {
                    return route.serverInit ? (route.serverInit(state, match)) : Promise.resolve(null)
                });
                Promise.all(promises).then((_res_) => {
                    let r_sObj = _res_[0]||{};
                    global.app.locals.__INITIAL_STATE__ = JSON.stringify(state);
                    const rootComp = (
                        <Root layout={layoutJson[r_sObj.layout]} url={params.url} routes={routes} states={state} type="server"/>
                    );
                    resolve(renderToString(rootComp));
                }).catch((err) => {
                    reject(err);
                })
            }).catch(_err=>{
                reject(_err);
            });
        });
        // return new Promise((resolve, reject)=>{
        //     match({
        //         routes : routeRenders(),
        //         location: params.url
        //     }, (err, redirectLocation, renderProps) => {
        //         if (err) {
        //             reject(err);
        //         } else if (redirectLocation) {
        //             reject(new Error('is redirectLocation'));
        //         } else if (renderProps) {
        //             onEnter(renderProps,state).then(() => {
        //                 global.app.locals.__INITIAL_STATE__ = JSON.stringify(state);
        //                 const rootComp = (
        //                     <Root states={state} renderProps={renderProps} type="server"/>
        //                 );
        //                 resolve(renderToString(rootComp));
        //             }).catch((_err)=>{
        //                 reject(_err);
        //             });
        //         } else {
        //             reject(new Error(`bu zhi dao ${params.url} ****`));
        //         }
        //     })
        // });
    }
}
